package com.szsfm.vehiclemanagement.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhoubigbo on 2017/11/16.
 */
public class MScrollView extends ScrollView {
    /**
     * 这些View不会被拦截
     */
    private List<View> mOtherViews;

    public MScrollView(Context context) {
        super(context);
    }

    public MScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
//        if (mOtherViews != null && mOtherViews.size() > 0) {
//            return inArea(ev);
//        }
        return super.onInterceptTouchEvent(ev);
    }

    public void addInterceptView(View view) {
        if (mOtherViews == null) mOtherViews = new ArrayList<>();
        mOtherViews.add(view);
    }


    /**
     * 测试view是否在点击范围内
     *
     * @param event
     * @return
     */
    private boolean inArea(MotionEvent event) {
        for (View v : mOtherViews) {
            float x = event.getRawX();
            float y = event.getRawY();
            int[] locate = new int[2];
            v.getLocationOnScreen(locate);
            int l = locate[0];
            int r = l + v.getWidth();
            int t = locate[1];
            int b = t + v.getHeight();
            if (x <= r && y <= b && x >= l && y >= t) //表示手指触摸的范围在View的点击范围内
                return false;
            else continue;
        }
        return true;
    }

}
